<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class WechatMiniLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $req = new Request();
        $token = $req->header('token');
        if ($token) {
            if (Cache::get($token)) {
                return $next($request);
            } else {
                return [
                    'status' => 'failed',
                    'code' => 401,
                    'msg' => 'token已过期'
                ];
            }
        } else {
            return [
                'status' => 'failed',
                'code' => 401,
                'msg' => '请求缺少token'
            ];
        }
    }
}
